<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button class="post_btn">Post</button>

    <script>
      let btn = document.querySelector(".post_btn");

      btn.addEventListener("click", (evt) => {
        sendPost("http://localhost:3000/login");
      });

      function sendPost(path) {
        // ajax四步走
        let xhr = new XMLHttpRequest();

        xhr.open("POST", path);
        xhr.setRequestHeader("Content-Type", "application/json");

        xhr.onload = () => {
          if (xhr.status === 200) {
            console.log(JSON.parse(xhr.response));
          } else {
            console.log("err");
          }
        };

        xhr.send(
          JSON.stringify({
            name: "张三",
            age: "18",
          })
        );
      }
    </script>
  </body>
</html>
